<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="handleClose">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <el-tabs v-model="activeName" class="demo-tabs" :before-leave="handleTabClick">
                <el-tab-pane label="基本信息" name="a">
                    <basicForm ref="basicFormRef" />
                </el-tab-pane>
                <el-tab-pane label="联系人" name="b">
                    <linkman v-if="activeName=='b'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane label="银行账户信息" name="c">
                    <xCorpbank v-if="activeName=='c'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane label="安全审核" name="d">
                    <xSecurity v-if="activeName=='d'" :companyName="companyName" :formOperate="baTable.form.operate" />
                </el-tab-pane>
                <el-tab-pane label="证件信息" name="e">
                    <xCertificate v-if="activeName=='e'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane v-if="type==2" label="车辆信息" name="f">
                    <xCarinfo v-if="activeName=='f'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane v-if="type==2" label="司押人员信息" name="g">
                    <xDriver v-if="activeName=='g'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane label="门点地址" name="h">
                    <aREA v-if="activeName=='h'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane v-if="type==2" label="审批流程" name="i">
                    <xCorpsq v-if="activeName=='i'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
                <el-tab-pane v-if="type==1" label="货物管理" name="j">
                    <goods v-if="activeName=='j'" :companyName="companyName" :formOperate="baTable.form.operate"/>
                </el-tab-pane>
            </el-tabs>
        </el-scrollbar>
        <template #footer v-if="activeName=='a'&&baTable.form.operate !=='view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, computed ,provide,watch} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { ElForm, ElMessage, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'
import createAxios from '/@/utils/axios'
import basicForm from './basicForm.vue'
import linkman from '../linkman/index.vue'
import xCorpbank from '../xCorpbank/index.vue'
import xSecurity from '../xSecurity/index.vue'
import xCertificate from '../xCertificate/index.vue'
import aREA from '../aREA/index.vue'
import goods from '../goods/index.vue'
import xCarinfo from '../xCarinfo/index.vue'
import xDriver from '../xDriver/index.vue'
import xCorpsq from '../xCorpsq/index.vue'
import { useRoute } from 'vue-router'


// const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    REMIND: [buildValidatorData({ name: 'required', title: t('corplib.REMIND') })],
    CORPNAME: [buildValidatorData({ name: 'required', title: t('corplib.CORPNAME') })],
    SUBDT: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.SUBDT') }) })],
    AUDITDT: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.AUDITDT') }) })],
    OPTIME: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.OPTIME') }) })],
})

// const companyName = ref("")

const companyName = computed(()=>{
    return baTable.form.items!.CORP
})

const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

provide('companyName',companyName)

const activeName = ref('a')

const basicFormRef = ref()
const handleSave = ()=>{
    basicFormRef.value.doSave()
}

//判断条件最后要调整
const handleTabClick=()=>{
    if(companyName.value){
        return true
    }else{
        ElMessage({
            message:'请先保存表单',
            type:'warning'
        })
        return false
    }
}

const handleClose = ()=>{
    baTable.toggleForm()
    activeName.value = 'a'
}

</script>

<style scoped lang="scss"></style>
